<?php

/**
 * @copyright 2011
 * @version $Id$
 * @access public
 * @author oriotiori

 */

class Admin_Model_Manufacturers extends Zend_Db_Table_Abstract{

    protected $_name = 'manufacturers';
    
    /**
     * Obtiene manufacturers
     * @return array $data
     */
    public function getManufacturers()
    {
    	$TO = new Admin_Model_TO_Manufacturers();
    	$data=array();
    	
    	try{
    		$sql=$this->getAdapter()->select()
    					->from(array('m' => TABLE_PREFIX.$this->_name), "*")
    					->joinLeft(array('i' => TABLE_PREFIX.'images'), "i.Id=m.Id_Image", array('Thumb'));
    		 
    		$res = $this->getAdapter()->fetchAll($sql);
    		$rowCount=count($res);
    			
    		if($rowCount>0)
    		{
    			foreach($res as $dt){
    				$TO->setId($dt['Id']);
    				$TO->setThumb($dt['Thumb']);
    				$TO->setManufacturer($dt['Manufacturer']);
    				$TO->setUrl($dt['Url']);
    				$TO->add();
    			}
    			$data = $TO->getAllData();
    			return $data;
    		}
    	}catch(Exception $e)
    	{
    		$error=array($e->getFile(), 'Exception: '.$e->getMessage());
    		$ExceptionHandler= new Default_Model_ExceptionHandler();
    		$ExceptionHandler->saveErrors($error);
    	}
    	return false;
    }
    
    
    
    /**
     * @param int $id
     * @return int $lastid
     */
    public function readManufacturerById($id=0)
    {
    	$TO = new Admin_Model_TO_Manufacturers();
    	$data=array();
    	try{
    		$sql=$this->getAdapter()->select()
    					->from(array('m' => TABLE_PREFIX.$this->_name), "*")
    					->joinLeft(array('i' => TABLE_PREFIX.'images'), "i.Id=m.Id_Image", array('Thumb'));    
    		$sql = $sql->where("m.Id=?", $id);
    
    		$res = $this->getAdapter()->fetchAll($sql);
    		$rowCount=count($res);
    			
    		if($rowCount>0)
    		{
    			foreach($res as $dt){
    				$TO->setId($dt['Id']);
    				$TO->setThumb($dt['Thumb']);
    				$TO->setIdImage($dt['Id_Image']);
    				$TO->setManufacturer($dt['Manufacturer']);
    				$TO->setUrl($dt['Url']);
    			}
    			$data = $TO->getAllVars();
    			return $data;
    		}
    	}catch(Exception $e)
    	{
    		$error=array($e->getFile(), 'Exception: '.$e->getMessage());
    		$ExceptionHandler= new Default_Model_ExceptionHandler();
    		$ExceptionHandler->saveErrors($error);
    	}
    	return false;
    }
    
    
    /**
     * @param Array $to
     * @return int
     */
    public function addManufacturer($to)
    {
        try{
            $data = array(
                  	'Manufacturer'  => $to['Manufacturer'],
                  	'Url'  => $to['Url']
            );

            $res = $this->getAdapter()->insert(TABLE_PREFIX.$this->_name ,$data);
            $lastId= $this->getAdapter()->lastInsertId();
            return $lastId;

        }catch(Exception $e)
        {
            $error=array($e->getFile(), 'Exception: '.$e->getMessage());
            $ExceptionHandler= new Default_Model_ExceptionHandler();
            $ExceptionHandler->saveErrors($error);
        }
        return false;
    }

    
    /**
    * @param Array $to
     * @return int
    */
    public function updateManufacturer($to)
    {
	    try{
		    $data = array(
		    	'Manufacturer'  => $to['Manufacturer'],
		    	'Url'  			=> $to['Url']		     
		    );
		    $where[]="Id=".$to['Id'];
		    
		    $res = $this->getAdapter()->update(TABLE_PREFIX.$this->_name ,$data, $where);
		    return $res;
		    
	    }catch(Exception $e)
	    {
		    $error=array($e->getFile(), 'Exception: '.$e->getMessage());
		    $ExceptionHandler= new Default_Model_ExceptionHandler();
		    $ExceptionHandler->saveErrors($error);
	    }
	    return false;
    }
    
    
    /**
    * @param Array $to
     * @return boolean
    */
    public function AddImageDB($to)
    {
	    try{
		    $data = array(
		    	'Id_Image'  => (int)$to['IdPicture']
		    );

		    $where[]="Id=".$to['IdManufacturer'];
		    $res = $this->getAdapter()->update(TABLE_PREFIX.$this->_name ,$data, $where);
		    return $res;
		    
	    }catch(Exception $e)
	    {
		    $error=array($e->getFile(), 'Exception: '.$e->getMessage());
		    $ExceptionHandler= new Default_Model_ExceptionHandler();
		    $ExceptionHandler->saveErrors($error);
	    }
	    return false;
    }
    
    
    /**
    * Crea Thumb
     * @param Array $to
    * @return boolean
    */
    public function CreateThumb($to)
    {
    	
	    try{
		    $imageHandler= new Admin_Model_ImageHandler();
		    $imageHandler->CreateThumb($to);
		    return true;
	    }catch(Exception $e)
	    {
	    	$error=array($e->getFile(), 'Exception: '.$e->getMessage());
	    	$ExceptionHandler= new Default_Model_ExceptionHandler();
	    	$ExceptionHandler->saveErrors($error);
	    }
    	return false;
    }
    
    
    
    /**
    * @param int id
    * @return boolean
    */
    public function deleteManufacturerById($id=0)
    {
	    try{
		    $facadeImage= new Admin_Model_Facade_ImageHandler();
		    $dataCat=$this->readManufacturerById($id);
		    
		    $res = $this->getAdapter()->delete(TABLE_PREFIX.$this->_name ,"Id=".$id);
		    $facadeImage->deleteImage($to['IdImage']);
		    
		    return $res;
	    }catch(Exception $e)
	            {
		    $error=array($e->getFile(), 'Exception: '.$e->getMessage());
		    $ExceptionHandler= new Default_Model_ExceptionHandler();
	        $ExceptionHandler->saveErrors($error);
	    }
	    return false;
    }
    
    
    
    
}
